package com.fengye.algorithms.leecode;

import java.util.Scanner;

/**
 * @Author fengye
 * @Date 2023/3/9 14:44
 **/
public class RomanToInt12And13Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            System.out.println(intTORoman(sc.nextInt()));
        }
    }

    public static String intTORoman(int num) {
        StringBuilder sb = new StringBuilder();
            int t = num / 1000;
            if(t > 0) {
                for (int i = 1; i <= t; i++) {
                    sb.append("M");
                }
            }
            num -= t*1000;
            if(num >= 900) {
                sb.append("CM");
                num -= 900;
            }

            if (num >= 500) {
                sb.append("D");
                num -= 500;
            }

            if(num >= 400) {
                sb.append("CD");
                num -= 400;
            }
            if (num >= 100) {
                t = num / 100;
                for(int i = 1; i <= t;i++) {
                    sb.append("C");
                }
                num -= t*100;
            }

            if(num >= 90) {
                sb.append("XC");
                num -= 90;
            }

            if(num >= 50) {
                sb.append("L");
                num -= 50;
            }

            if(num >= 40) {
                sb.append("XL");
                num -= 40;
            }

            if(num >= 10) {
                t = num / 10;
                for(int i = 1; i <= t;i++) {
                    sb.append("X");
                }
                num -= t * 10;
            }

            if(num == 9) {
                sb.append("IX");
                num -= 9;
            }

            if(num >= 5) {
                sb.append("V");
                num -= 5;
            }

            if(num == 4) {
                sb.append("IV");
            } else {
                for(int i = 0; i < num; i++) {
                    sb.append("I");
                }
            }

        return sb.toString();
    }

    public static int romanToInt(String s) {
        char[] arr = s.toCharArray();
        int res = 0;
        char[] point = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int max = 0;
        for(int i=arr.length - 1; i>=0;i--) {
            char c = arr[i];
            if(c=='I') {
                if(max == 0) {
                    res += map(c);
                } else {
                    res -= map(c);
                }
            } else if(c == 'V') {
                res += map(c);
                max = 1;
            } else if(c == 'X'){
                if(max <= 2) {
                    res += map(c);
                    max = 2;
                } else {
                    res -= map(c);
                }
            } else if (c == 'L') {
                res += map(c);
                max = 3;
            } else if (c == 'C') {
                if(max <= 4) {
                    res += map(c);
                    max = 4;
                } else {
                    res -= map(c);
                }
            } else if (c == 'D') {
                res += map(c);
                max = 5;
            } else if (c == 'M') {
                res += map(c);
                max = 6;
            }
        }
        return res;
    }

    /**
     * I             1
     * V             5
     * X             10
     * L             50
     * C             100
     * D             500
     * M             1000
     *
     * 来源：力扣（LeetCode）
     * 链接：https://leetcode.cn/problems/roman-to-integer
     * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     * @param c
     * @return
     */
    public static int map(char c) {
        switch(c) {
            case 'I' :
                return 1;
            case 'V' :
                return 5;
            case 'X' :
                return 10;
            case 'L' :
                return 50;
            case 'C' :
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default: return 0;
        }
    }
}
